package com.example.a10908.retorfitrxjava.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.example.a10908.retorfitrxjava.R;
import com.example.a10908.retorfitrxjava.model.bean.LoginBean;
import com.example.a10908.retorfitrxjava.presenter.Presenter_Login;
import com.example.a10908.retorfitrxjava.ui.view.ILoginView;
import com.example.a10908.retorfitrxjava.utils.SnackbarUtil;

public class LoginActivity extends AppCompatActivity implements ILoginView, View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private EditText ed1_name;
    private TextInputLayout textInputLayout1;
    private EditText ed2_password;
    private TextInputLayout textInputLayout12;
    private CheckBox remerber;
    private CheckBox loginCheck;
    private Button btn;
    private Presenter_Login presenter_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        presenter_login = new Presenter_Login(this);
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getPassWord() {
        return null;
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public void setPassword(String password) {

    }

    @Override
    public void loginState(LoginBean loginBean) {
        if (loginBean.getLoginstate().equals("OK")) {

            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
        } else {
            SnackbarUtil.show(btn, "账号密码错误", 0);
        }
    }

    private void initView() {
        ed1_name = (EditText) findViewById(R.id.ed1_name);
        textInputLayout1 = (TextInputLayout) findViewById(R.id.textInputLayout1);
        ed2_password = (EditText) findViewById(R.id.ed2_password);
        textInputLayout12 = (TextInputLayout) findViewById(R.id.textInputLayout12);
        remerber = (CheckBox) findViewById(R.id.remerber);
        loginCheck = (CheckBox) findViewById(R.id.loginCheck);
        btn = (Button) findViewById(R.id.btn);

        remerber.setOnCheckedChangeListener(this);
        loginCheck.setOnCheckedChangeListener(this);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String name = ed1_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            SnackbarUtil.show(btn, "Username", 1);
            return;
        }

        String password = ed2_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            SnackbarUtil.show(btn, "Password", 1);
            return;
        }

        // TODO validate success, do something

        presenter_login.setiLoginInfo(name, password);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
